﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Quartz;
using Quartz.Impl;

public class QuartzHelper {
    private IScheduler _scheduler;

    public QuartzHelper() {
        // 创建一个调度器工厂
        ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
        // 获取调度器实例
        _scheduler = schedulerFactory.GetScheduler().Result;
    }
    public async Task Start() {
        if (!_scheduler.IsStarted) {
            // 启动调度器
            await _scheduler.Start();
        }
    }

    public async Task Stop() {
        if (_scheduler.IsStarted) {
            // 关闭调度器
            await _scheduler.Shutdown(true);
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="jobName"></param>
    /// <param name="groupName"></param>
    /// <param name="triggerName"></param>
    /// <param name="triggerGroupName"></param>
    /// <param name="cronExpression"></param>
    /// <param name="jobAction"></param>
    public void ScheduleJob<T>(string jobName, string groupName, string triggerName, string triggerGroupName, string cronExpression,  Action<string> jobAction = null) where T : IJob {
        // 创建一个作业
        IJobDetail job = JobBuilder.Create<T>()
            .WithIdentity(jobName, groupName)
            .Build();

        if (jobAction != null) {
            job.JobDataMap.Add("action", jobAction);
        }
        // 创建一个触发器
        ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
            .WithIdentity(triggerName, triggerGroupName)
            .WithCronSchedule(cronExpression)
            .Build();

        // 将作业和触发器关联起来，并将其添加到调度器中
        _scheduler.ScheduleJob(job, trigger);
    }
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="jobName"></param>
    /// <param name="groupName"></param>
    /// <param name="triggerName"></param>
    /// <param name="triggerGroupName"></param>
    /// <param name="cronExpression"></param>
    /// <param name="jobAction"></param>
    public void ScheduleJob<T>(string jobName, string groupName, string triggerName, string triggerGroupName, string cronExpression, Action jobAction = null) where T : IJob {
        // 创建一个作业
        IJobDetail job = JobBuilder.Create<T>()
            .WithIdentity(jobName, groupName)
            .Build();

        if (jobAction != null) {
            job.JobDataMap.Add("action", jobAction);
        }
        // 创建一个触发器
        ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
            .WithIdentity(triggerName, triggerGroupName)
            .WithCronSchedule(cronExpression)
            .Build();

        // 将作业和触发器关联起来，并将其添加到调度器中
        _scheduler.ScheduleJob(job, trigger);
    }
    public void DeleteJob(string jobName, string groupName) {
        // 根据作业名称和作业组名删除作业
        _scheduler.DeleteJob(new JobKey(jobName, groupName));
    }

    public List<string> GetJobGroupNames() {
        // 获取所有作业组名列表
        return (List<string>)_scheduler.GetJobGroupNames().Result;
    }

    public List<string> GetTriggerGroupNames() {
        // 获取所有触发器组名列表
        return (List<string>)_scheduler.GetTriggerGroupNames().Result;
    }
}
